package logic

import (
	"context"
	"meituan-waimai/models/mysql"

	"meituan-waimai/meituanrpc/internal/svc"
	"meituan-waimai/meituanrpc/meituanrpc"

	"github.com/zeromicro/go-zero/core/logx"
)

type GoodsListLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewGoodsListLogic(ctx context.Context, svcCtx *svc.ServiceContext) *GoodsListLogic {
	return &GoodsListLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *GoodsListLogic) GoodsList(in *meituanrpc.GoodsListRequest) (*meituanrpc.GoodsListResponse, error) {
	// todo: add your logic here and delete this line
	var goodsList []mysql.Goods
	query := mysql.DB.Model(&mysql.Goods{})
	//支持按分类、销量、价格等维度排序
	if in.Cate == "true" { //分类排序，true为降序
		query = query.Order("goods_cate desc")
	} else if in.Cate == "false" { //false为升序
		query = query.Order("goods_cate asc")
	}
	if in.SalesVolume == "true" { //销量排序，true为降序
		query = query.Order("goods_sales_volume desc")
	} else if in.SalesVolume == "false" { //false为升序
		query = query.Order("goods_sales_volume asc")
	}
	if in.Price == "true" { //价格排序，true为降序
		query = query.Order("goods_price desc")
	} else if in.Price == "false" { //false为升序
		query = query.Order("goods_price asc")
	}
	query.Find(&goodsList)
	var list []*meituanrpc.GoodsList
	for _, goods := range goodsList {
		slice := meituanrpc.GoodsList{
			GoodsId:          int64(goods.ID),
			GoodsName:        goods.GoodsName,
			GoodsCate:        goods.GoodsCate,
			GoodsSalesVolume: goods.GoodsSalesVolume,
			GoodsPrice:       float32(goods.GoodsPrice),
		}
		list = append(list, &slice)
	}
	return &meituanrpc.GoodsListResponse{
		List: list,
	}, nil
}
